package com.zoffy.wmsbackend.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zoffy.wmsbackend.common.ErrorCode;
import com.zoffy.wmsbackend.common.RequestParam;
import com.zoffy.wmsbackend.exception.BusinessException;
import com.zoffy.wmsbackend.mapper.StockOutOrderMapper;
import com.zoffy.wmsbackend.mapper.WarehouseMapper;
import com.zoffy.wmsbackend.model.dto.stockOut.StockOutOrderDto;
import com.zoffy.wmsbackend.model.dto.stockOut.StockOutDTO;
import com.zoffy.wmsbackend.model.dto.stockOut.StockOutDetailDTO;
import com.zoffy.wmsbackend.model.entity.*;
import com.zoffy.wmsbackend.model.excel.StockOutImportListener;
import com.zoffy.wmsbackend.model.vo.stock.stockOut.StockOutDetailVo;
import com.zoffy.wmsbackend.model.vo.stock.stockOut.StockOutOrderDetailsVo;
import com.zoffy.wmsbackend.model.vo.stock.stockOut.StockOutOrderVo;
import com.zoffy.wmsbackend.service.*;
import com.zoffy.wmsbackend.utils.StockUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 86170
* @description 针对表【stock_out_order(出存表)】的数据库操作Service实现
* @createDate 2025-03-01 02:27:09
*/
@Service
public class StockOutOrderServiceImpl extends ServiceImpl<StockOutOrderMapper, StockOutOrder>
    implements StockOutOrderService {

    @Resource
    private GoodsService goodsService;
    @Resource
    private WarehouseMapper warehouseMapper;
    @Resource
    private StockOutOrderDetailService stockOutOrderDetailService;
    @Resource
    private StockUtil stockUtil;
    @Resource
    private StockOutOrderMapper stockOutOrderMapper;

    @Override
    @Transactional
    public Boolean stockOut(StockOutDTO stockOutDTO) {
        String stockOutNo = stockOutDTO.getStockOutNo();
        String warehouseNo = stockOutDTO.getWarehouseNo();
        List<StockOutDetailDTO> stockOutDetails = stockOutDTO.getStockOutDetails();
        Map<String, Goods> goodsMap = goodsService.list().stream().collect(Collectors.toMap(Goods::getGoodsCode, k -> k));
        List<StockOutOrderDetail> stockOutOrderDetails= new ArrayList<>();
        LambdaQueryWrapper<Warehouse> queryWrapper= Wrappers.lambdaQuery();
        queryWrapper.eq(Warehouse::getWarehouseCode,warehouseNo);
        Warehouse warehouse = warehouseMapper.selectOne(queryWrapper);
        StockOutOrder stockOutOrder = new StockOutOrder();
        stockOutOrder.setStockOutNo(stockOutNo);
        stockOutOrder.setWarehouseId(warehouse.getId());
        stockOutOrder.setStatus(0);
        this.save(stockOutOrder);
        for (StockOutDetailDTO stockOutDetail : stockOutDetails){
            StockOutOrderDetail stockOutOrderDetail=new StockOutOrderDetail();
            stockOutOrderDetail.setStockOutOrderId(stockOutOrder.getId());
            stockOutOrderDetail.setStockOutDetailNo(stockOutDetail.getStockOutDetailNo());
            stockOutOrderDetail.setWarehouseId(warehouse.getId());
            stockOutOrderDetail.setGoodsId(goodsMap.get(stockOutDetail.getGoodsNo()).getId());
            stockOutOrderDetail.setQuantity(stockOutDetail.getNum());
            stockOutOrderDetail.setStatus(0);
            stockOutOrderDetails.add(stockOutOrderDetail);
        }
        try{
            stockUtil.subStock(stockOutOrderDetails);
        }catch (Exception e){
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"库存不足");
        }
        stockOutOrderDetailService.saveBatch(stockOutOrderDetails);
        return true;
    }

    @Override
    public Boolean importStockOut(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(),null,new StockOutImportListener());
        return true;
    }

    @Override
    public Page<StockOutOrderVo> listStockOutOrderVoByPage(RequestParam<StockOutOrderDto> requestParam) {
        Long pageSize = requestParam.getPageSize();
        Integer pageNum = requestParam.getPageNum();
        StockOutOrderDto dto = requestParam.getParams();
        Page<StockOutOrderVo> page=new Page<>(pageNum,pageSize);
        Page<StockOutOrderVo> res = stockOutOrderMapper.listStockOutVoByPage(page, dto);
        return res;
    }

    @Override
    public Boolean approval(Long stockOutId) {
        StockOutOrder stockOutOrder = this.getById(stockOutId);
        stockOutOrder.setStatus(90);
        this.updateById(stockOutOrder);
        LambdaQueryWrapper<StockOutOrderDetail> queryWrapper=Wrappers.lambdaQuery();
        queryWrapper.eq(StockOutOrderDetail::getStockOutOrderId,stockOutId);
        return true;
    }

    @Override
    public StockOutOrderDetailsVo getDetailsById(Long stockOutId) {
        StockOutOrderVo stockOutVo = stockOutOrderMapper.getStockOutVoById(stockOutId);
        List<StockOutDetailVo> details = stockOutOrderMapper.getDetailsById(stockOutId);
        StockOutOrderDetailsVo stockOutOrderDetailsVo = new StockOutOrderDetailsVo();
        stockOutOrderDetailsVo.setId(stockOutVo.getId());
        stockOutOrderDetailsVo.setWarehouseCode(stockOutVo.getWarehouseCode());
        stockOutOrderDetailsVo.setWarehouseName(stockOutVo.getWarehouseName());
        stockOutOrderDetailsVo.setWarehouseId(stockOutVo.getWarehouseId());
        stockOutOrderDetailsVo.setStockOutNo(stockOutVo.getStockOutNo());
        stockOutOrderDetailsVo.setDetails(details);
        return stockOutOrderDetailsVo;
    }
}




